<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * viewmember.php
 * Began: Thu December 19 2002
 * 
 * $Id: viewmember.php 541 2008-05-20 06:56:16Z rspeicher $
 * 
 ******************************/
 
define('EQDKP_INC', true);
$eqdkp_root_path = './';
include_once($eqdkp_root_path . 'common.php');

// Start session management
$user->start();
$user->setup();
$user->check_auth('u_member_view');

if ( (isset($_GET[URI_NAME])) && (strval($_GET[URI_NAME] != '')) )
{
    $sql = 'SELECT member_id, member_name, member_earned, member_spent, member_adjustment, (member_earned-member_spent+member_adjustment) As member_current, member_lastraid
            FROM ' . MEMBERS_TABLE . "
            WHERE member_name='".$_GET[URI_NAME]."'";
            
    if ( !($member_result = $db->query($sql)) )
    {
        message_die('Could not obtain member information', '', __FILE__, __LINE__, $sql);
    }
        
    // Make sure they provided a valid member name
    if ( !$member = $db->fetch_record($member_result) )
    {
        message_die($user->lang['error_invalid_name_provided']);
    }
    
    // Find the percent of raids they've attended in the last 30 and 90 days
    $thirty_days = mktime(0, 0, 0, date('m'), date('d')-30, date('Y'));
    $ninety_days = mktime(0, 0, 0, date('m'), date('d')-90, date('Y'));
    
    $raid_count_30 = $db->query_first('SELECT count(*) FROM ' . RAIDS_TABLE . ' WHERE (raid_date BETWEEN '.$thirty_days.' AND '.time().')');
    $raid_count_90 = $db->query_first('SELECT count(*) FROM ' . RAIDS_TABLE . ' WHERE (raid_date BETWEEN '.$ninety_days.' AND '.time().')');
    
    $sql = 'SELECT count(*)
            FROM ' . RAIDS_TABLE . ' r, ' . RAID_ATTENDEES_TABLE . " ra
            WHERE (ra.raid_id = r.raid_id) 
            AND (ra.member_name='".$member['member_name']."')
            AND (r.raid_date BETWEEN ".$thirty_days." AND ".time().")";
    $individual_raid_count_30 = $db->query_first($sql);
    
    $sql = 'SELECT count(*)
            FROM ' . RAIDS_TABLE . ' r, ' . RAID_ATTENDEES_TABLE . " ra
            WHERE (ra.raid_id = r.raid_id) 
            AND (ra.member_name='".$member['member_name']."')
            AND (r.raid_date BETWEEN ".$ninety_days." AND ".time().")";
    $individual_raid_count_90 = $db->query_first($sql);
    
    $percent_of_raids_30 = ( $raid_count_30 > 0 ) ? round(($individual_raid_count_30 / $raid_count_30) * 100) : 0;
    $percent_of_raids_90 = ( $raid_count_90 > 0 ) ? round(($individual_raid_count_90 / $raid_count_90) * 100) : 0;
    
    //
    // Raid Attendance
    //
    $rstart = ( isset($_GET['rstart']) ) ? $_GET['rstart'] : 0;
    
    // Find $current_earned based on the page.  This prevents us having to pass the
    // current earned as a GET variable which could result in user error
    if ( (!isset($_GET['rstart'])) || ($_GET['rstart'] == '0') )
    {
        $current_earned = $member['member_earned'];
    }
    else
    {
        $current_earned = $member['member_earned'];
        $sql = 'SELECT raid_value
                FROM ' . RAIDS_TABLE . ' r, ' . RAID_ATTENDEES_TABLE . " ra
                WHERE (ra.raid_id = r.raid_id) 
                AND (ra.member_name='".$member['member_name']."')
                ORDER BY r.raid_date DESC
                LIMIT ".$rstart;
        if ( !($earned_result = $db->query($sql)) )
        {
            message_die('Could not obtain raid information', '', __FILE__, __LINE__, $sql);
        }
        while ( $ce_row = $db->fetch_record($earned_result) )
        {
            $current_earned -= $ce_row['raid_value'];
        }
        $db->free_result($earned_result);
    }
    
    $sql = 'SELECT r.raid_id, r.raid_name, r.raid_date, r.raid_note, r.raid_value
            FROM ' . RAIDS_TABLE . ' r, ' . RAID_ATTENDEES_TABLE . " ra
            WHERE (ra.raid_id = r.raid_id) 
            AND (ra.member_name='".$member['member_name']."')
            ORDER BY r.raid_date DESC
            LIMIT ".$rstart.','.$user->data['user_rlimit'];
    if ( !($raids_result = $db->query($sql)) )
    {
        message_die('Could not obtain raid information', '', __FILE__, __LINE__, $sql);
    }
    while ( $raid = $db->fetch_record($raids_result) )
    {
        $tpl->assign_block_vars('raids_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'DATE' => ( !empty($raid['raid_date']) ) ? date($user->style['date_notime_short'], $raid['raid_date']) : '&nbsp;',
            'U_VIEW_RAID' => 'viewraid.php'.$SID.'&amp;' . URI_RAID . '='.$raid['raid_id'],
            'NAME' => ( !empty($raid['raid_name']) ) ? stripslashes($raid['raid_name']) : '&lt;<i>Not Found</i>&gt;',
            'NOTE' => ( !empty($raid['raid_note']) ) ? stripslashes($raid['raid_note']) : '&nbsp;',
            'EARNED' => $raid['raid_value'],
            'CURRENT_EARNED' => sprintf("%.2f", $current_earned))
        );
        $current_earned -= $raid['raid_value'];
    }
    $db->free_result($raids_result);
    $sql = 'SELECT count(*)
            FROM ' . RAIDS_TABLE . ' r, ' . RAID_ATTENDEES_TABLE . " ra
            WHERE (ra.raid_id = r.raid_id) 
            AND (ra.member_name='".$member['member_name']."')";
    $total_attended_raids = $db->query_first($sql);
    
    //
    // Item Purchase History
    //
    $istart = ( isset($_GET['istart']) ) ? $_GET['istart'] : 0;
    
    if ( (!isset($_GET['istart'])) || ($_GET['istart'] == '0') )
    {
        $current_spent = $member['member_spent'];
    }
    else
    {
        $current_spent = $member['member_spent'];
        $sql = 'SELECT item_value 
                FROM ' . ITEMS_TABLE . " 
                WHERE (item_buyer='".$member['member_name']."') 
                ORDER BY item_date DESC 
                LIMIT ".$istart;
        if ( !($spent_result = $db->query($sql)) )
        {
            message_die('Could not obtain raid information', '', __FILE__, __LINE__, $sql);
        }
        while ( $cs_row = $db->fetch_record($spent_result) )
        {
            $current_spent -= $cs_row['item_value'];
        }
        $db->free_result($spent_result);
    }
    
    $sql = 'SELECT i.item_id, i.item_name, i.item_value, i.item_date, i.raid_id, r.raid_name
            FROM ' . ITEMS_TABLE . ' i, ' . RAIDS_TABLE . " r
            WHERE (r.raid_id = i.raid_id) AND (i.item_buyer='".$member['member_name']."') 
            ORDER BY i.item_date DESC
            LIMIT ".$istart.','.$user->data['user_ilimit'];
    if ( !($items_result = $db->query($sql)) )
    {
        message_die('Could not obtain item information', 'Database error', __FILE__, __LINE__, $sql);
    }
    while ( $item = $db->fetch_record($items_result) )
    {
        $tpl->assign_block_vars('items_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'DATE' => ( !empty($item['item_date']) ) ? date($user->style['date_notime_short'], $item['item_date']) : '&nbsp;',
            'U_VIEW_ITEM' => 'viewitem.php'.$SID.'&amp;' . URI_ITEM . '='.$item['item_id'],
            'U_VIEW_RAID' => 'viewraid.php'.$SID.'&amp;' . URI_RAID . '='.$item['raid_id'],
            'NAME' => stripslashes($item['item_name']),
            'RAID' => ( !empty($item['raid_name']) ) ? stripslashes($item['raid_name']) : '&lt;<i>Not Found</i>&gt;',
            'SPENT' => $item['item_value'],
            'CURRENT_SPENT' => sprintf("%.2f", $current_spent))
        );
        $current_spent -= $item['item_value'];
    }
    $db->free_result($items_result);
    
    $total_purchased_items = $db->query_first('SELECT count(*) FROM ' . ITEMS_TABLE . " WHERE item_buyer='".$member['member_name']."' ORDER BY item_date DESC");
    
    //
    // Individual Adjustment History
    //
    $sql = 'SELECT adjustment_value, adjustment_date, adjustment_reason
            FROM ' . ADJUSTMENTS_TABLE . "
            WHERE member_name='".$member['member_name']."'
            ORDER BY adjustment_date DESC";
    if ( !($adjustments_result = $db->query($sql)) )
    {
        message_die('Could not obtain adjustment information', '', __FILE__, __LINE__, $sql);
    }
    while ( $adjustment = $db->fetch_record($adjustments_result) )
    {
        $tpl->assign_block_vars('adjustments_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'DATE' => ( !empty($adjustment['adjustment_date']) ) ? date($user->style['date_notime_short'], $adjustment['adjustment_date']) : '&nbsp;',
            'REASON' => ( !empty($adjustment['adjustment_reason']) ) ? stripslashes($adjustment['adjustment_reason']) : '&nbsp;',
            'C_INDIVIDUAL_ADJUSTMENT' => color_item($adjustment['adjustment_value']),
            'INDIVIDUAL_ADJUSTMENT' => $adjustment['adjustment_value'])
        );
    }
       
    $tpl->assign_vars(array(
        'GUILDTAG' => $eqdkp->config['guildtag'],
        'NAME' => $member['member_name'],
        
        'L_EARNED' => $user->lang['earned'],
        'L_SPENT' => $user->lang['spent'],
        'L_ADJUSTMENT' => $user->lang['adjustment'],
        'L_CURRENT' => $user->lang['current'],
        'L_RAIDS_30_DAYS' => sprintf($user->lang['raids_x_days'], 30),
        'L_RAIDS_90_DAYS' => sprintf($user->lang['raids_x_days'], 90),
        'L_RAID_ATTENDANCE_HISTORY' => $user->lang['raid_attendance_history'],
        'L_DATE' => $user->lang['date'],
        'L_NAME' => $user->lang['name'],
        'L_NOTE' => $user->lang['note'],
        'L_EARNED' => $user->lang['earned'],
        'L_CURRENT' => $user->lang['current'],
        'L_ITEM_PURCHASE_HISTORY' => $user->lang['item_purchase_history'],
        'L_RAID' => $user->lang['raid'],
        'L_INDIVIDUAL_ADJUSTMENT_HISTORY' => $user->lang['individual_adjustment_history'],
        'L_REASON' => $user->lang['reason'],
        'L_ADJUSTMENT' => $user->lang['adjustment'],
        
        'EARNED' => $member['member_earned'],
        'SPENT' => $member['member_spent'],
        'ADJUSTMENT' => $member['member_adjustment'],
        'CURRENT' => $member['member_current'],
        'RAIDS_30_DAYS' => sprintf($user->lang['of_raids'], $percent_of_raids_30),
        'RAIDS_90_DAYS' => sprintf($user->lang['of_raids'], $percent_of_raids_90),

        'C_ADJUSTMENT' => color_item($member['member_adjustment']),
        'C_CURRENT' => color_item($member['member_current']),
        'C_RAIDS_30_DAYS' => color_item($percent_of_raids_30, true),
        'C_RAIDS_90_DAYS' => color_item($percent_of_raids_90, true),
        
        'RAID_FOOTCOUNT' => sprintf($user->lang['viewmember_raid_footcount'], $total_attended_raids, $user->data['user_rlimit']),
        'RAID_PAGINATION' => generate_pagination('viewmember.php'.$SID.'&amp;name='.$member['member_name'].'&amp;istart='.$istart, $total_attended_raids, $user->data['user_rlimit'], $rstart, 'rstart'),
        'ITEM_FOOTCOUNT' => sprintf($user->lang['viewmember_item_footcount'], $total_purchased_items, $user->data['user_ilimit']),
        'ITEM_PAGINATION' => generate_pagination('viewmember.php'.$SID.'&amp;name='.$member['member_name'].'&amp;rstart='.$rstart, $total_purchased_items, $user->data['user_ilimit'], $istart, 'istart'),
        'ADJUSTMENT_FOOTCOUNT' => sprintf($user->lang['viewmember_adjustment_footcount'], $db->num_rows($adjustments_result)))
    );
    
    $db->free_result($adjustments_result);
    
    $page_title = sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.sprintf($user->lang['viewmember_title'], $member['member_name']);
    include_once($eqdkp_root_path . 'includes/page_header.php');
    
    $tpl->set_filenames(array(
        'body' => 'viewmember.html')
    );
    
    include_once($eqdkp_root_path . 'includes/page_tail.php');
}
else
{
    message_die($user->lang['error_invalid_name_provided']);
}
?>